package oauth

import (
	"errors"
	"xcloud-oauth2-server/models"

	"gorm.io/gorm"
)

var (
	// ErrRoleNotFound ...
	ErrRoleNotFound = errors.New("Role not found")
)

// FindRoleByID looks up a role by ID and returns it
func (s *Service) FindRoleByID(id string) (*models.OauthRole, error) {
	role := new(models.OauthRole)
	result := s.db.Where("id = ?", id).First(role)

	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
		return nil, ErrRoleNotFound
	}

	//if s.db.Where("id = ?", id).First(role).RecordNotFound() {
	//	return nil, ErrRoleNotFound
	//}
	return role, nil
}
